package com.nohgun.bike.pass;


import com.nohgun.bike.MainActivity;
import com.nohgun.bike.R;
import com.nohgun.bike.database.DBHelper;



import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

public class PassMainActivity extends Activity {
	
	protected DBHelper dbhelper;
	private SQLiteDatabase db;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pass_main);
        
        this.dbhelper = new DBHelper(getApplicationContext());
        if (this.db == null) {
            this.db = this.dbhelper.getWritableDatabase();
        }
        
        Spinner sp1 = (Spinner)findViewById(R.id.spinner1);
        ArrayAdapter<CharSequence> ad1 =  ArrayAdapter.createFromResource(this,R.array.passtime,android.R.layout.simple_spinner_dropdown_item);
        ad1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        sp1.setAdapter(ad1);
        //sp1.getSelectedItemPosition();
        
        final Button pass_button_start =  (Button)findViewById(R.id.pass_button_start);
        pass_button_start.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				
				Cursor c = null;
				
				try {
					Spinner sp1 = (Spinner)findViewById(R.id.spinner1);
					EditText  pass_editText_title = (EditText)findViewById(R.id.pass_editText_title);
					
		    		c = db.rawQuery(" SELECT  MAX(pass_id)+1 AS CNT FROM pass_inf ; ", null);
		    		c.moveToFirst();
		    		int pass_id = c.getInt(0);
		    		
					Log.d("imageCount ================",""+ String.valueOf(sp1.getSelectedItemPosition()));
					Log.d("imageCount ================",""+ pass_editText_title.getText().toString());
					
					StringBuilder qSB = new StringBuilder();
					qSB.append(" INSERT INTO pass_inf ");
					qSB.append(" ( pass_id , pass_title , time_interval ) ");
					qSB.append(" VALUES (");
					qSB.append(pass_id);
					qSB.append(",'").append(pass_editText_title.getText().toString()).append("'");
					qSB.append(",").append(sp1.getSelectedItemPosition());
					qSB.append(");");
					
					db.execSQL(qSB.toString());
					
					Log.d("###debug::pass_button_start ###",""+ qSB.toString());
					

					Intent in = new Intent(PassMainActivity.this,PassInfActivity.class);
					in.putExtra("pass_id", pass_id);
					startActivity(in);
					finish();
				} catch (SQLException e) {
		    		Log.e("","- List<Memo> getMemoList()", e );
		    	} finally {
		    		if (c != null && !c.isClosed()) {
		    			c.close();
		    		}
		    	}
			}
		    	
		});
        
    }
}
